<template>
  <div class="block">
    <el-container>
      <!-- 头部区域 -->
      <el-header>
        <div class="logo">
          <img src="../assets/logo.png" alt="logo" />
          <span>后台管理系统</span>
        </div>
        <el-button type="info" class="exit" @click="exit">退出</el-button>
      </el-header>
      <!-- 页面主题区域 -->
      <el-container class="home-container">
        <!-- 侧边栏区域 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 侧边栏菜单区域 -->
          <el-menu
            background-color="#313743"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <!-- 一级菜单 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <i :class="icons[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + child.path"
                v-for="child in item.children"
                :key="child.id"
                @click="saveNavAction('/' + child.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ child.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主题区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  created() {
    //获取到菜单列表
    this.getMenuList()
    // 添加保存的活跃路径
    this.activePath = window.sessionStorage.getItem('actionpath')
  },
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      isCollapse: false,
      // 图标字体
      icons: {
        '125': 'iconfont icon-UserSettings',
        '103': 'iconfont icon-moxing-miaobian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-dingdan',
        '145': 'iconfont icon-tongji'
      },
      activePath: ''
    }
  },
  components: {},
  methods: {
    // 退出登录
    exit() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/Login')
    },
    // 获取菜单数据资源
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status != 200) return res.meat.msg
      this.menuList = res.data
    },

    // 切换折叠
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    // 保存当前活跃的路由，nav高亮显示
    saveNavAction(actionpath) {
      // 在寄存器中存储当前活跃的路径
      window.sessionStorage.setItem('actionpath', actionpath)
      this.activePath = actionpath
    }
  }
}
</script>

<style scoped lang="less">
/* 主体样式 */
.home-container {
  height: 100vh;
}

.el-header {
  background-color: #373c41;
  display: flex;
  color: white;
  justify-content: space-between;
  font-size: 20px;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 50px;
      background-color: rgba(224, 250, 132, 0.932);
      border-radius: 50%;
    }
    span {
      margin-left: 25px;
    }
  }
  .exit {
    height: 40px;
  }
}
.el-aside {
  background-color: rgb(49, 55, 67);
  transition: width 0.5s;
  .el-menu {
    border-right: 0;
    .iconfont {
      font-size: 17px;
      margin-right: 12px;
    }
  }
}
// 切换状态栏
.toggle-button {
  background-color: rgb(71, 81, 99);
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
  text-align: center;
}
.el-main {
  height: 100%vh;
  background-color: rgb(233, 237, 241);
}
</style>
